package linked

import "fmt"

type ListNode struct {
	Val  int
	Next *ListNode
}

func NewListNode() *ListNode {
	return new(ListNode)
}

func (ln *ListNode) PushArray(list []int) {
	for _, item := range list {
		ln.AddNode(&ListNode{Val: item})
	}
}

func (ln *ListNode) GetHead() *ListNode {
	return ln
}

func (ln *ListNode) GetTail() *ListNode {
	tail := ln
	for {
		if tail.Next == nil {
			return tail
		} else {
			tail = tail.Next
		}
	}
}

func (ln *ListNode) AddNode(head *ListNode) {
	for {
		if ln.Next != nil {
			ln =  ln.Next
		} else {
			if ln.Val == 0 {
				ln.Val = head.Val
			} else {
				ln.Next = head
			}
			break
		}
	}
}

func (ln *ListNode) ToArray(head *ListNode) []int {
	tmp := make([]int, 0)
	tmp = append(tmp, head.Val)
	for head.Next != nil {
		head = head.Next
		tmp = append(tmp, head.Val)
	}
	return tmp
}

func (ln *ListNode) Print() {
	tmp := ln.ToArray(ln.GetHead())
	fmt.Println(tmp)
}

func (ln *ListNode) Del(head *ListNode) {
	return
}

func getLength(head *ListNode) (length int) {
	for ; head != nil; head = head.Next {
		length++
	}
	return
}
